package com.crm.service.client.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.dao.client.ClientInfoDao;
import com.crm.dao.client.ClientTransferDao;
import com.crm.pojo.ClientInfo;
import com.crm.pojo.ClientTransfer;
import com.crm.service.client.ClientTransferService;
import com.crm.vo.Result;
@Service
@Transactional(rollbackFor = Exception.class)
public class ClientTransferServiceImpl implements ClientTransferService{

	@Autowired
	public ClientTransferDao clientTransferDao;
	@Autowired
	public ClientInfoDao clientInfoDao;
	
	@Override
	public Result transferClient(BigDecimal out_userId, BigDecimal in_userId, Long cid,
			BigDecimal operateClientId) {
		try {
			ClientInfo clientInfo = clientInfoDao.findOne(cid);
			clientInfo.setUserId(in_userId);
			clientInfo.setUpdateDate(new Date());
			clientInfoDao.save(clientInfo);
			
			ClientTransfer clientTransfer = new ClientTransfer();
			clientTransfer.setInClientId(in_userId);
			clientTransfer.setOutClentId(out_userId);
			clientTransfer.setClientInfo(clientInfo);
			clientTransfer.setOperateClientId(operateClientId);
			clientTransfer.setOperateDate(new Timestamp(System.currentTimeMillis()));
			clientTransfer.setClientTransferIsDel("否");
			clientTransferDao.save(clientTransfer);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("亲，转移客户失败！");
		}
	}

	@Override
	public List<ClientTransfer> queryAllClientTransfer(int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return clientTransferDao.queryAllClientTransfer(page);
	}

	@Override
	public List<ClientTransfer> queryClientTransferByInClientId(BigDecimal inClientId, int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return clientTransferDao.queryClientTransferByInClientId(inClientId, page);
	}

	@Override
	public List<ClientTransfer> queryClientTransferByOutClientId(BigDecimal outClentId, int curpage, int pagesize) {
		PageRequest page = new PageRequest(curpage - 1, pagesize);
		return clientTransferDao.queryClientTransferByOutClientId(outClentId, page);
	}

	@Override
	public Long queryCountAllClientTransfer() {
		return clientTransferDao.queryCountAllClientTransfer();
	}

	@Override
	public Long queryCountClientTransferByInClientId(BigDecimal inClientId) {
		return clientTransferDao.queryCountClientTransferByInClientId(inClientId);
	}

	@Override
	public Long queryCountClientTransferByOutClientId(BigDecimal outClentId) {
		return clientTransferDao.queryCountClientTransferByOutClientId(outClentId);
	}

	
}
